﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using System.Data;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Customer.Manage
{
    public partial class Profile : System.Web.UI.Page
    {
        UserDAL user = new UserDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUtility.UserGUID == null || WebUtility.UserGUID == "0")
            {
                Response.Clear();
                Response.Write("<script>parent.ResetURL('/Customer/signin.aspx?next=" + Server.UrlEncode("/Customer/Index.aspx") + "');</script>");
                Response.End();
            }

            if (!IsPostBack)
            {
                UserInfo userinfo = user.GetUserByUserGuid(WebUtility.UserGUID);
                if (userinfo != null)
                {
                    tb_OldPassword.Value = Utility.Encryption.Decrypt(userinfo.Password);

                    tb_FirstName.Text = userinfo.FirstName;
                    tb_LastName.Text = userinfo.LastName;
                    tb_BusinessName.Text = userinfo.BusinessName;
                    tb_AddressLine1.Text = userinfo.AddressLine1;
                    tb_AddressLine2.Text = userinfo.AddressLine2;
                    tb_City.Text = userinfo.City;
                    ddl_State.SelectedValue = userinfo.State;
                    tb_Zipcode.Text = userinfo.Zipcode;

                    DataTable dtSetting = user.GetSettingByUserGUID(WebUtility.UserGUID);

                    if (dtSetting.Select("FlagName='Logo'").Length > 0)
                    {
                        img_Logo.ImageUrl = dtSetting.Select("FlagName='Logo'")[0]["Value"].ToString();
                    }
                }
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            UserInfo userinfo = user.GetUserByUserGuid(WebUtility.UserGUID);
            if (userinfo != null)
            {
                userinfo.FirstName = tb_FirstName.Text.Trim();
                userinfo.LastName = tb_LastName.Text.Trim();
                userinfo.AddressLine1 = tb_AddressLine1.Text.Trim();
                userinfo.AddressLine2 = tb_AddressLine2.Text.Trim();
                userinfo.BusinessName = tb_BusinessName.Text.Trim();
                userinfo.City = tb_City.Text.Trim();
                userinfo.State = ddl_State.SelectedValue; ;
                userinfo.Zipcode = tb_Zipcode.Text.Trim();

                user.UpdateUser(userinfo, "UPDATE");

                if (file_Logo.HasFile)
                {
                    string path = "/Customer/assets/" + DateTime.Now.Ticks + ".jpg";
                    file_Logo.PostedFile.SaveAs(Server.MapPath(path));

                    UserSettingInfo settingInfo = new UserSettingInfo();
                    settingInfo.UserGUID = WebUtility.UserGUID;
                    settingInfo.FlagName = "Logo";
                    settingInfo.Value = path;

                    user.UpdateSetting(settingInfo);
                }

                Response.Redirect("Profile.aspx");
            }
        }

        protected void btn_ChangePassword_Click(object sender, EventArgs e)
        {
            lb_Msg_ConfirmPassword.Text = lb_Msg_NewPassword.Text = lb_Msg_Password.Text = "";

            UserInfo userinfo = user.GetUserByUserGuid(WebUtility.UserGUID);
            if (userinfo != null)
            {
                if (tb_Password.Text.Trim() == Utility.Encryption.Decrypt(userinfo.Password))
                {
                    if (tb_NewPassword.Text.Trim() != "")
                    {
                        if (tb_NewPassword.Text.Trim() == tb_ConfirmPassword.Text.Trim())
                        {
                            userinfo.Password = Utility.Encryption.Encrypt(tb_NewPassword.Text.Trim());
                            user.UpdateUser(userinfo, "UPDATE");

                            Response.Redirect("Profile.aspx");
                        }
                        else
                        {
                            lb_Msg_ConfirmPassword.Text = "Please check your password";
                        }
                    }
                    else
                    {
                        lb_Msg_NewPassword.Text = "Please type in a valid password";
                    }
                }
                else
                {
                    lb_Msg_Password.Text = "Invalid password";
                }
            }
        }
    }
}